package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.InStockConverter;
import com.goods.business.converter.SupplierConverter;
import com.goods.business.mapper.*;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.*;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @Author: 赵勇镔
 * @Time: 2021/6/9 10:51
 * @Description:
 */
@Service
public class InStockServiceImpl implements InStockService {

    @Autowired(required = false)
    private InStockMapper inStockMapper;

    @Autowired(required = false)
    private SupplierMapper supplierMapper;

    @Autowired(required = false)
    private ProductMapper productMapper;

    @Autowired(required = false)
    private InStockInfoMapper inStockInfoMapper;

    @Autowired(required = false)
    private ProductStockMapper productStockMapper;




    //入库记录列表查询
    @Override
    public PageVO<InStockVO> findInStockList(Integer pageNum, Integer pageSize, InStockVO inStockVO1) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);

        Integer status = inStockVO1.getStatus();
        String inNum = inStockVO1.getInNum();
        Date startTime = inStockVO1.getStartTime();
        Date endTime = inStockVO1.getEndTime();
        Integer type = inStockVO1.getType();

        Example o = new Example(InStock.class);
        Example.Criteria criteria = o.createCriteria();


        criteria.andLike("status", "%" + status + "%");
        if (inNum != null && !"".equals(inNum)) {
            criteria.andLike("inNum", "%" + inNum + "%");
        }
        if (type != null) {
            criteria.andLike("type", "%" + type + "%");
        }
        if (startTime != null && endTime != null) {
            criteria.andGreaterThan("createTime", startTime);
            criteria.andLessThan("createTime", endTime);
        }


        List<InStock> inStocks = inStockMapper.selectByExample(o);
        List<InStockVO> inStockVOSs = new ArrayList<>();
        //循环查到的列表
        for (InStock inStock : inStocks) {
            InStockVO inStockVO = InStockConverter.converterProductVO(inStock);

            //通过inStock表中的supplierId查询supplier表中的应的数据 然赋值
            Long supplierId = inStockVO.getSupplierId();
            Example o2 = new Example(Supplier.class);
            o2.createCriteria().andEqualTo("id", supplierId);
            Supplier supplier = supplierMapper.selectOneByExample(o2);

            inStockVO.setAddress(supplier.getAddress());
            inStockVO.setSupplierName(supplier.getName());
            inStockVO.setEmail(supplier.getEmail());
            inStockVO.setContact(supplier.getContact());
            inStockVO.setPhone(supplier.getPhone());
            inStockVO.setType(inStock.getType());
            inStockVO.setOperator(inStock.getOperator());


            inStockVOSs.add(inStockVO);
        }
        //转化为分页对象
        PageInfo<InStock> Info = new PageInfo<>(inStocks);
        //前端返回需要的对象
        return new PageVO<>(Info.getTotal(), inStockVOSs);
    }

    //入库明细
    @Override
    public InStockDetailVO detail(Integer pageNum, Integer id) {

        //创建最终返回对象
        InStockDetailVO inStockDetailVO = new InStockDetailVO();

        //1.通过id查stock对象
        Example o = new Example(InStock.class);
        o.createCriteria().andEqualTo("id", id);
        //stock对象
        InStock inStock = inStockMapper.selectOneByExample(o);

        //2.拿到对象查询关联表和supplier表
        Example o1 = new Example(Supplier.class);
        o1.createCriteria().andEqualTo("id", inStock.getSupplierId());
        //supplier表对象
        Supplier supplier = supplierMapper.selectOneByExample(o1);
        //转化为supplierVO封装到明细表
        SupplierVO supplierVO = SupplierConverter.converterSupplierVO(supplier);
        inStockDetailVO.setSupplierVO(supplierVO);


        Example o2 = new Example(StockInfo.class);
        o2.createCriteria().andEqualTo("inNum", inStock.getInNum());
        //中间表对象集合
        List<StockInfo> stockInfos = inStockInfoMapper.selectByExample(o2);


        //3.通过关联表拿到product表数据
        //3.1先遍历中间表 用每个中间表的pNum查询对应到product 然后封装到iteamVOList中
        List<InStockItemVO> inStockItemVOS = new ArrayList<>();
        for (StockInfo stockInfo : stockInfos) {

            Example o3 = new Example(Product.class);
            o3.createCriteria().andEqualTo("pNum", stockInfo.getPNum());
            Product product = productMapper.selectOneByExample(o3);

            InStockItemVO inStockItemVO = new InStockItemVO();
            BeanUtils.copyProperties(product, inStockItemVO);

            inStockItemVO.setCount(stockInfo.getProductNumber());

            inStockItemVOS.add(inStockItemVO);
        }

        //数据整合封装返回
        inStockDetailVO.setItemVOS(inStockItemVOS);
        inStockDetailVO.setTotal(inStockItemVOS.size());
        inStockDetailVO.setInNum(inStock.getInNum());
        inStockDetailVO.setType(inStock.getType());
        inStockDetailVO.setOperator(inStock.getOperator());
        inStockDetailVO.setStatus(inStock.getStatus());


        return inStockDetailVO;
    }

    //入库列表加入回收站 0入库 1回收站 2待审核
    @Override
    public void remove(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(1);
        inStockMapper.updateByPrimaryKey(inStock);

    }

    //入库列表回收站还原
    @Override
    public void back(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock);
    }

    //入库列表回收站删除
    @Override
    public void delete(Long id) {
        inStockMapper.deleteByPrimaryKey(id);
    }

    //入库信息保存
    @Override
    public void addIntoStock(InStockVO inStockVO) {

        //创建入库表 把主要信息保存
        InStock inStock = new InStock();
        inStock.setCreateTime(new Date());
        inStock.setModified(new Date());
        inStock.setProductNumber(inStockVO.getProducts().size());
        inStock.setType(inStockVO.getType());
        inStock.setOperator("admin");
        inStock.setRemark(inStockVO.getRemark());
        inStock.setStatus(2);

        //1先判断SupplierVO有无id
        //2无id先保存Supplier表 获取id @GeneratedValue(generator = "JDBC")
        if (inStockVO.getSupplierId() == null) {
            Supplier supplier = new Supplier();
            BeanUtils.copyProperties(inStockVO,supplier);
            supplier.setCreateTime(new Date());
            supplier.setModifiedTime(new Date());
            supplierMapper.insert(supplier);
            inStock.setSupplierId(supplier.getId());
        } else {
            //有id直接保存
            inStock.setSupplierId(inStockVO.getSupplierId());
        }

        //3把id保存在入库表 生成in_num
        String inNum = UUID.randomUUID().toString();
        inStock.setInNum(inNum);

        //保存入库表
        inStockMapper.insert(inStock);

        //4获取分项表的p_num 保存到中间表
        List<Object> products = inStockVO.getProducts();
        for (Object product : products) {
            //获取遍历的Map
            Map product1 = (Map) product;
            //强转类型 获取明细数量
            int productNumber = (int)product1.get("productNumber");
            //强转类型获取id
            Object productId =  product1.get("productId");
            //通过id查询物资资料表
            Product product2 = productMapper.selectByPrimaryKey(productId);
            StockInfo stockInfo = new StockInfo();
            stockInfo.setProductNumber(productNumber);
            stockInfo.setInNum(inNum);
            //获取pNum并保存
            stockInfo.setPNum(product2.getPNum());
            stockInfo.setCreateTime(new Date());
            stockInfo.setModifiedTime(new Date());
            inStockInfoMapper.insert(stockInfo);
        }
    }

    //审核入库
    @Override
    public void publish(Long id) {
        //根据id查询入库表
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        //获取inNum
        String inNum = inStock.getInNum();

        //通过inNum获取物资中间表
        Example o = new Example(StockInfo.class);
        o.createCriteria().andEqualTo("inNum",inNum);
        List<StockInfo> stockInfos = inStockInfoMapper.selectByExample(o);
        for (StockInfo stockInfo:stockInfos) {
            //获取pNum
            String pNum = stockInfo.getPNum();
            //获取累加值
            Integer productNumber = stockInfo.getProductNumber();
            //通过pum查询biz_product_stock物资总和表
            Example o2 = new Example(ProductStock.class);
            o2.createCriteria().andEqualTo("pNum",pNum);
            ProductStock productStock = productStockMapper.selectOneByExample(o2);
            //累加商品总数
            productStock.setStock(productStock.getStock()+productNumber);
            //保存在总和表中
            productStockMapper.updateByPrimaryKey(productStock);
        }

        //更新入库信息状态 并保存
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock);
    }

}

